<?php

class Emms_Form_Loan_PaymentPrincipal extends HTech_Form
{
    protected $_primary  = 'PaymentId';
    protected $_minAmount;
    protected $_maxAmount;
                    
    public function getValues()
    {
        $data = parent::getValues();
        return $data;
    }
    
    public function __construct(Emms_EarlyPayment $pmt) 
    {
        parent::__construct(); 
        
        $this->_minAmount = $pmt->getMinAmount();
        $this->_maxAmount = $pmt->getMaxAmount();
        
        $this->setName('PaymentPrincipal');
        
        $this->setAttrib('title', $this->translate("Payment form"));
        $this->setDescription($this->translate("Payment towards principal"));
          
        $paymentId = new Zend_Form_Element_Hidden('PaymentId');
                       
        $amount = new Zend_Form_Element_Text('Amount');
        $amount->setLabel("Payment amount")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid amount")))
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkAmountCallback'),
                'messages' => $this->translate("Invalid amount")));  
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type' => 'submit')));      
        
        $this->addElements(array($paymentId, $amount, $submit));
    }
    
    public function checkAmountCallback($amount) 
    {
        if($amount > $this->_maxAmount) return false;
        if($amount < $this->_minAmount) return false;
        return true;
    }
}